//
//  SettingPWViewController.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/21.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "SettingPWViewController.h"
#import "LoginViewModel.h"
#import "CustomPopOverView.h"

@interface SettingPWViewController ()<CustomPopOverViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UIImageView * dmImageView;
@property (nonatomic, strong) UIImageView * bgcImageView;
@property (nonatomic, strong) UILabel * welcomeLB;
@property (nonatomic, strong) UIImageView * incIV;
@property (nonatomic, strong) UIImageView * identityIV;
@property (nonatomic, strong) UITextField * incTF;
@property (nonatomic, strong) UILabel * choiceIdentity;
@property (nonatomic, strong) UIButton * pullDownBTN;
@property (nonatomic, strong) UIButton * validationBTN;
@end

@implementation SettingPWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BGCOLOR;
    self.titleLabel.text = @"设置密码";
    [self setUpAssembleView];
}

#pragma mark - 创建视图
- (void)setUpAssembleView{
    
    //logo
    UIImageView * dmImageView = [UIImageView new];
    self.dmImageView = dmImageView;
    [self.contentView addSubview:self.dmImageView];
    self.dmImageView.sd_layout.widthIs(GetScreenWidth*0.504).heightIs(GetScreenHeight*0.07).topSpaceToView(self.contentView,GetScreenHeight*0.063).leftSpaceToView(self.contentView,GetScreenWidth*0.248);
    self.dmImageView.image = [UIImage imageNamed:@"damao.cn"];
    
    // 背景
    UIImageView * bgcImageView = [UIImageView new];
    self.bgcImageView = bgcImageView;
    [self.contentView addSubview:self.bgcImageView];
    self.bgcImageView.sd_layout.widthIs(GetScreenWidth-44).heightIs(GetScreenHeight*0.442).topSpaceToView(self.contentView,GetScreenHeight*0.205).leftSpaceToView(self.contentView,22);
    self.bgcImageView.image = [UIImage imageNamed:@"bg-white"];
    
    UILabel * welcomeLB = [UILabel new];
    self.welcomeLB= welcomeLB;
    [self.contentView addSubview:self.welcomeLB];
    self.welcomeLB.sd_layout.widthIs(GetScreenWidth*0.42).heightIs(25).topSpaceToView(self.contentView,GetScreenHeight*0.235).leftSpaceToView(self.contentView,GetScreenWidth*0.29);
    self.welcomeLB.text = @"欢迎您注册大猫网";
    self.welcomeLB.textColor = UIColorFromRGB(0x4a4a4a);
    self.welcomeLB.font = [UIFont systemFontOfSize:19.0f];


    
    // 公司
    UIImageView * incIV = [UIImageView new];
    self.incIV = incIV;
    [self.contentView addSubview:self.incIV];
    self.incIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.welcomeLB,GetScreenHeight*0.0374).leftSpaceToView(self.contentView,46);
    self.incIV.image = [UIImage imageNamed:@"bg-yz"];
    
    UILabel * incLB = [UILabel new];
    [self.contentView addSubview:incLB];
    incLB.sd_layout.widthIs(50).heightIs(20).topSpaceToView(self.welcomeLB,GetScreenHeight*0.0524).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    incLB.text = @"公司:";
    incLB.font = [UIFont systemFontOfSize:15];
    incLB.textColor = UIColorFromRGB(0x808080);
    
    UITextField * incTF = [UITextField new];
    self.incTF = incTF;
    [self.contentView addSubview:self.incTF];
    self.incTF.sd_layout.widthIs(GetScreenWidth*0.5).heightIs(20).topSpaceToView(self.welcomeLB,GetScreenHeight*0.0524).leftSpaceToView(self.contentView,GetScreenWidth*0.33);
    self.incTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.incTF.font = [UIFont systemFontOfSize:15];
    self.incTF.placeholder = @"请输入您的公司名称";
    self.incTF.delegate = self;
    self.incTF.textColor = UIColorFromRGB(0xb9b9b9);
    
    
    // 身份
    UIImageView * identityIV = [UIImageView new];
    self.identityIV = identityIV;
    [self.contentView addSubview:self.identityIV];
    self.identityIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.incIV,GetScreenHeight*0.0299).leftSpaceToView(self.contentView,46);
    self.identityIV.image = [UIImage imageNamed:@"bg-yz"];
    
    UILabel * identityLB = [UILabel new];
    [self.contentView addSubview:identityLB];
    identityLB.sd_layout.widthIs(50).heightIs(20).topSpaceToView(self.incIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    identityLB.text = @"身份:";
    identityLB.font = [UIFont systemFontOfSize:15];
    identityLB.textColor = UIColorFromRGB(0x808080);
    
    UILabel  * choiceIdentity = [UILabel new];
    self.choiceIdentity = choiceIdentity;
    [self.contentView addSubview:self.choiceIdentity];
    self.choiceIdentity.sd_layout.widthIs(GetScreenWidth*0.43).heightIs(20).topSpaceToView(self.incIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.33);
    self.choiceIdentity.font = [UIFont systemFontOfSize:15];
    self.choiceIdentity.textColor = UIColorFromRGB(0xb9b9b9);
    
    UIButton * pullDownBTN = [UIButton new];
    self.pullDownBTN = pullDownBTN;
    [self.contentView addSubview:self.pullDownBTN];
    self.pullDownBTN.sd_layout.widthIs(GetScreenHeight*0.0599).heightIs(GetScreenWidth*0.106).topSpaceToView(self.incIV,GetScreenHeight*0.030).leftSpaceToView(self.contentView,GetScreenWidth*0.755);
    [self.pullDownBTN setImage:[UIImage imageNamed:@"icon-arrow"] forState:UIControlStateNormal];
    [self.pullDownBTN addTarget:self action:@selector(clickPullDownBTN:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton * validationBTN = [UIButton new];
    self.validationBTN = validationBTN;
    [self.contentView addSubview:validationBTN];
    self.validationBTN.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.choiceIdentity,GetScreenHeight*0.0569).leftSpaceToView(self.contentView,46);
    [self.validationBTN setTitle:@"完成并登录" forState:UIControlStateNormal];
    [self.validationBTN setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [self.validationBTN setBackgroundImage:[UIImage imageNamed:@"btn-dl"] forState:UIControlStateNormal];
    [self.validationBTN addTarget:self action:@selector(validationSettingPassWord) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 完成并登录
- (void)validationSettingPassWord
{
    [self.incTF resignFirstResponder];
    
    if (self.incTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入公司名称"];
    }else if (self.choiceIdentity.text.length == 0){
        [SVProgressHUD showInfoWithStatus:@"请选择身份"];
    }else{
        NSDictionary *jsonDict = @{@"mobile":self.numberPhone,@"password":self.password,@"auth_code":self.authCode,@"company":self.incTF.text,@"create_time":[SecurityUtil timeStamp]};
        //开始注册
        [LoginViewModel oprationClient:jsonDict type:@"0" success:^{
            //注册成功直接进入个人主页或者首页 TO DO ..
            [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];
        }];
    }
}

- (void)clickPullDownBTN:(UIButton *)btn{
    [self.incTF resignFirstResponder];
    NSArray * titleArray = @[@"供应商",@"采购商", @"服务商", @"供应商、采购商"];
    CustomPopOverView * pView = [[CustomPopOverView alloc]initWithBounds:CGRectMake(GetScreenHeight*0.0599, CGRectGetMaxY(self.pullDownBTN.frame)+13, GetScreenWidth*0.637, 40*4) Row_Height:40 TitleMenus:titleArray Images:nil FontSize:15 TitleColor:UIColorFromRGB(0x808080)];
    pView.delegate = self;
    pView.containerBackgroudColor = UIColorFromRGB(0xf6f6f6);
    [pView showFrom:btn alignStyle:CPAlignStyleRight];
    
}
- (void)popOverView:(CustomPopOverView *)pView didClickMenuIndex:(NSInteger)index titles:(NSString *)title{
    [pView removeFromSuperview];
    self.choiceIdentity.text = title;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.incTF resignFirstResponder];
    return YES;
}
//点击屏幕空白处去掉键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.incTF resignFirstResponder];
}

@end
